#!/bin/bash
#
# $Id: get_profile_info.sh 120 2011-02-17 09:11:00Z mamartynov@gmail.com $
#

. $OPENARS_HOME/common/debug.h
. $OPENARS_HOME/common/functions.h

user=$USER_ARS
[ -z "$user" ] && user=$LOGNAME

:>$OUTFILE

# $OUTFILE --> CMDB
function update_db() {
if [ -z "$CONNECT_ARS" ]; then
    ERR "not defined CONNECT_ARS environment for Oracle connect. exit."
    exit 1;
fi
$ORACLE_HOME/bin/sqlplus -s /NOLOG <<END
    set feedback off
    connect $CONNECT_ARS
    @"$OUTFILE"
    commit;
    exit;
END
    return 0
}

srvlist=`get_hosts --control=YES`
INF "count hosts: `echo $srvlist | wc -w`"

put_file="$PRJDIR/profile_info.local.sh"
ssh_command="./profile_info.local.sh; rm ./profile_info.local.sh"

for host in $srvlist
do
    (
    sftp $user@$host &> /dev/null << END
put $put_file
END
    #server_info=`ssh $user@$host "$ssh_command"`
    SSH $user@$host "$ssh_command" server_info
        [ -z "$server_info" ] && continue
        #echo "$server_info" >> $OUTFILE
	real_host=`echo "$server_info" | awk -F":" '/^HOST:/{print $2}'`
        os_version=`echo "$server_info" | awk -F":" '/^OS:/{print $2}'`
        ram=`echo "$server_info" | awk -F":" '/^RAM:/{print $2}'`
        cpu=`echo "$server_info" | awk -F":" '/^CPU:/{print $2}'`
        sn=""
        server_model=""
        internal_hdd=""

        [ -z "$real_host" ] && ERR "not get REAL_HOST information from $host"
        [ -z "$os_version" ] && ERR "not get OS_VERSION information from $host"
	[ -z "$ram" ] && ERR "not get RAM information from $host"
        [ -z "$cpu" ] && ERR "not get CPU information from $host"
        #[ -z "$sn" ] && ERR "not get SN information from $host"
        #[ -z "$internal_hdd" ] && ERR "not get HDD information from $host"
        #[ -z "$server_model" ] && ERR "not get SERVER_MODEL information from $host"

        echo "update hosts_profile set real_host=trim('$real_host'), os_version=trim('$os_version'), \
server_model=trim('$server_model'), \
cpu=trim('$cpu'), sn=trim('$sn'), internal_hdd=trim('$internal_hdd'), ram=trim('$ram') where host='$host';" >> $OUTFILE

    ) &
done
WAIT 180

update_db
if [ "$?" -ne 0 ]; then
    ERR "error function update_db()"
    exit 1
fi

exit 0


